package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorArticleDTO;
import com.pshcp.domain.model.DoctorArticle;
import com.pshcp.mapper.DoctorArticleMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生文章信息表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorArticleService {

    @Autowired
    private DoctorArticleMapper doctorArticleMapper;

    public PageList<DoctorArticle> list(DoctorArticleDTO doctorArticleDTO) {
        if (doctorArticleDTO.getPageNum() != null && doctorArticleDTO.getPageSize() != null) {
            PageHelper.startPage(doctorArticleDTO.getPageNum(), doctorArticleDTO.getPageSize());
        }
        Example example = new Example(DoctorArticle.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorArticleDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorArticleDTO.getStatus());
        } else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if (doctorArticleDTO.getDoctorId() != null) {
            criteria.andEqualTo("doctorId", doctorArticleDTO.getDoctorId());
        }
        if (CollectionUtils.isEmpty(doctorArticleDTO.getDeptIds())){
            criteria.andIn("deptId", doctorArticleDTO.getDeptIds());
        }
        if (!StringUtils.isBlank(doctorArticleDTO.getHospitalId())) {
            criteria.andEqualTo("hospitalId", doctorArticleDTO.getHospitalId());
        }
        if (doctorArticleDTO.getBizStatus() != null) {
            criteria.andEqualTo("bizStatus", doctorArticleDTO.getBizStatus());
        }
        //搜素
        if (!StringUtils.isBlank(doctorArticleDTO.getSearch())) {
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.orLike("title", "%" + doctorArticleDTO.getSearch() + "%");
            example.and(criteria1);
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorArticle> list = doctorArticleMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorArticle> select(DoctorArticle doctorArticle) {
        return doctorArticleMapper.select(doctorArticle);
    }

    public Long insert(DoctorArticle doctorArticle) {
        doctorArticle.setId(SystemUtil.getId());
        doctorArticle.setStatus(CommonStatusEnum.INIT.getCode());
        doctorArticle.setCreatedTime(new Date());
        doctorArticleMapper.insert(doctorArticle);
        return doctorArticle.getId();
    }

    public int update(DoctorArticle doctorArticle) {
        doctorArticle.setUpdateTime(new Date());
        return doctorArticleMapper.updateByPrimaryKeySelective(doctorArticle);
    }

    public DoctorArticle findById(Long id) {
        return doctorArticleMapper.selectByPrimaryKey(id);
    }

    public List<DoctorArticle> findByIds(List<Long> ids) {
        Example example = new Example(DoctorArticle.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorArticleMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorArticle.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorArticle doctorArticle = new DoctorArticle();
        doctorArticle.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorArticleMapper.updateByExampleSelective(doctorArticle, example);
    }

}
